﻿using UnityEngine;

public class NpcRandomStateBehaviour : StateMachineBehaviour
{
    [Header("参数名称")]
    public string ParamName;
    [Header("参数ID")]
    public int[] StateIdArray;

    private string playingTrigger;
    // OnStateEnter is called before OnStateEnter is called on any state inside this state machine
    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        
    }

    // OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
    //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateExit is called before OnStateExit is called on any state inside this state machine
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMove is called before OnStateMove is called on any state inside this state machine
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateIK is called before OnStateIK is called on any state inside this state machine
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMachineEnter is called when entering a state machine via its Entry Node
    override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
    {
        if (StateIdArray == null || StateIdArray.Length <= 0)
        {
            if (!string.IsNullOrEmpty(playingTrigger))
            {
                animator.ResetTrigger(playingTrigger);
            }

            if (!string.IsNullOrEmpty(ParamName))
            {
                animator.SetTrigger(ParamName);
            }
            playingTrigger = ParamName;
        }
        else
        {
            if (!string.IsNullOrEmpty(playingTrigger))
            {
                animator.ResetTrigger(playingTrigger);
            }
            int randomIndex = Random.Range(0, StateIdArray.Length);
            string trigger = ParamName + StateIdArray[randomIndex];
            animator.SetTrigger(trigger);
        }
    }

    // OnStateMachineExit is called when exiting a state machine via its Exit Node
    //override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
    //{
    //    
    //}
}
